package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.model.vo.NewCustomerVo;
import cn.edu.xmu.oomall.customer.service.NewCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/customer", produces = "application/json;charset=UTF-8")
/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:12:49
 */
public class NewCustomerAccountController {

    @Autowired
    private NewCustomerService newCustomerService;

    @Autowired
    private HttpServletResponse httpServletResponse;

    /**
     * 该方法只可在linux环境下测试，windows系统将无法通过测试！
     * @param newCustomerVo
     * @param bindingResult
     * @return Object
     */
    @PostMapping(value = "/customers")
    public Object registerCustomerAccount(@Validated @RequestBody NewCustomerVo newCustomerVo,
                                          BindingResult bindingResult) {
        var res = Common.processFieldErrors(bindingResult, httpServletResponse);
        if(res != null) {
            return res;
        }

        ReturnObject returnObject = newCustomerService.registerCustomerAccount(newCustomerVo);
        return Common.decorateReturnObject(returnObject);
    }
}
